﻿using System.Collections.Generic;
using GameObjects;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Network;

namespace StillAlive
{
    /// <summary>
    /// Здесь находится процесс игры (не меню)
    /// </summary>
    public class Battlefield : Microsoft.Xna.Framework.DrawableGameComponent
    {
        public static Battlefield Instance;

        SpriteBatch spriteBatch;
        // персонажи
        List<DrawableCharacter> players = new List<DrawableCharacter>();
        // наш игрок
        Character player;
        Communicator communicator;
        Sprite playerSprite;
        SpriteFont font;
        public int PacketLength;

        public Battlefield(Game game)
            : base(game)
        {
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);

            // текстуры
            Texture2D playerTexture = Game.Content.Load<Texture2D>(@"Images\player");
            playerSprite = new Sprite(
                playerTexture, new Point(32, 32), 0, Point.Zero, new Point(1, 1));
            communicator = new Communicator(out player);
            // шрифты
            font = Game.Content.Load<SpriteFont>(@"Fonts\SpriteFont1");

            base.LoadContent();
        }

        // создаём персонажа
        public DrawableCharacter CreateCharacter(int playerId)
        {
            DrawableCharacter character = new DrawableCharacter(playerId);
            character.Sprite = playerSprite;
            players.Add(character);
            return character;
        }

        public void RemoveCharacter(int playerId)
        {
            DrawableCharacter toRemove = players.Find(p => p.Id == playerId);
            players.Remove(toRemove);
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            foreach (var plr in players)
            {
                plr.Update(gameTime);
                //player.Move();
            }
            player.Move(gameTime);

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
            // рисуем игрока
            lock (players)
            {
                foreach (var character in players)
                {
                    character.Draw(gameTime, spriteBatch);
                }
            }
            string text = PacketLength == 0 ? "No connection." : "Packet length: " + PacketLength;
            spriteBatch.DrawString(font, text, new Vector2(10, 10), Color.Black);

            spriteBatch.End();

            base.Draw(gameTime);
        }

        protected override void Dispose(bool disposing)
        {
            communicator.Stop();
            base.Dispose(disposing);
        }
    }
}
